package com.lj.frame.menu;

import com.lj.tools.exception.YerException;
import com.lj.tools.response.Result;
import com.lj.frame.menu.entity.Menu;
import com.lj.frame.menu.service.impl.MenuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api(tags = "菜单")
@Slf4j
@RequiredArgsConstructor
@RequestMapping("menu")
@RestController
public class MenuController {

    private final MenuServiceImpl menuService;

    @ApiOperation(value = "获取菜单列表", notes = "", response = Result.class)
    @GetMapping("list")
    public Result  getList(){
        return Result.ok().setContent("查询成功", menuService.getMenuTree());
    }

    @ApiOperation(value = "菜单详情", notes = "", response = Result.class)
    @GetMapping("info/{id}")
    public Result  info(@PathVariable("id")Integer id){
        return Result.ok().setContent("查询成功", menuService.info(id));
    }


    @ApiOperation(value = "添加菜单", notes = "", response = Result.class)
    @PostMapping("add")
    public Result add(@Valid @RequestBody Menu menu) throws YerException {
        menuService.save(menu);
        return Result.ok().setContent("添加成功");
    }

    @ApiOperation(value = "更新菜单", notes = "", response = Result.class)
    @PostMapping("update")
    public Result update(@Valid @RequestBody Menu menu) throws YerException {
        menuService.save(menu);
        return Result.ok().setContent("更新成功");
    }

    @ApiOperation(value = "删除菜单", notes = "", response = Result.class)
    @PostMapping("remove")
    public Result remove(@Valid @RequestBody List<Integer> menuIdList) throws YerException {
        menuService.remove(menuIdList);
        return Result.ok().setContent("删除成功");
    }
}
